package com.kunchi.platform.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kunchi.platform.model.data.CurrentData;
import com.kunchi.platform.model.data.HistoryData;
import com.kunchi.platform.service.DataService;
import com.kunchi.platform.service.LoginService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

/**
 * Created by denniszhou on 2017/6/24.
 */
@Service("dataService")
public class DataServiceImpl implements DataService {
  @Resource
  LoginService loginService;

  @Value("${baseUrl}")
  private String baseUrl;

  @Value("${login.hash}")
  private String loginHash;

  @Autowired
  RestTemplate restTemplate;

  @Override
  public List<CurrentData> getCurrentData(String deviceId) throws IOException {
    List<CurrentData> historyDataList = new ArrayList<>();
    String url = baseUrl + "/currentdata?token={0}&hash={1}&deviceid={2}";
    String token = loginService.login();
    String response = restTemplate.getForObject(MessageFormat.format(
        url, token, loginHash, deviceId), String.class);
    JSONArray historyData = JSONObject.parseObject(response).getJSONArray("data");
    for (int i = 0; i < historyData.size(); i++) {
      historyDataList.add(JSONObject.parseObject(historyData.getString(i), CurrentData.class));
    }
    return historyDataList;
  }

  @Override
  public HashMap<String, List<HistoryData>> getHistoryData(
      String deviceId, Long startTimestamp, Long endTimestamp) throws IOException {
    HashMap<String, List<HistoryData>> historyDataHashMap = new HashMap<>();
    String url = baseUrl + "/history/forids?token={0}&hash={1}&deviceid={2}";
    String token = loginService.login();
    String response = restTemplate.getForObject(MessageFormat.format(
        url, token, loginHash, deviceId), String.class);
    JSONObject content = JSONObject.parseObject(response).getJSONObject("data");
    for (String itemId : content.keySet()) {
      JSONArray historyDataList = content.getJSONArray(itemId);
      for (int i = 0; i < historyDataList.size(); i++) {
        if (!historyDataHashMap.containsKey(itemId)) {
          historyDataHashMap.put(itemId, new ArrayList<>());
        } else {
          HistoryData historyData = JSON.parseObject(historyDataList.getString(i), HistoryData.class);
          if ((startTimestamp == null || historyData.getHtime().after(new Date(startTimestamp))) &&
              (endTimestamp == null || historyData.getHtime().before(new Date(endTimestamp)))) {
            historyDataHashMap.get(itemId).add(historyData);
          }
        }
      }
    }
    return historyDataHashMap;
  }


  @Override
  public List<HistoryData> getHistoryDataByDataItemId(
      String deviceId, String dataItemId, Long startTimestamp, Long endTimestamp) throws IOException {
    List<HistoryData> historyDataList = new ArrayList<>();
    String url = baseUrl + "/historydata?token={0}&hash={1}&deviceid={2}&dataitemid={3}";
    String token = loginService.login();
    String response = restTemplate.getForObject(MessageFormat.format(
        url, token, loginHash, deviceId, dataItemId), String.class);
    JSONArray historyDatas = JSONObject.parseObject(response).getJSONArray("data");
    for (int i = 0; i < historyDatas.size(); i++) {
      HistoryData historyData = JSONObject.parseObject(historyDatas.getString(i), HistoryData.class);
      if ((startTimestamp == null || historyData.getHtime().after(new Date(startTimestamp))) &&
          (endTimestamp == null || historyData.getHtime().before(new Date(endTimestamp)))) {
        historyDataList.add(historyData);
      }
    }
    return historyDataList;
  }
}
